//引入获取菜单权限的pinia
import { usePermission } from '../store/permission';
//引入路由
import router from '../router';
import { useBreadCrumb } from '../store/breadCrumb';
import { useUser } from '../store/users';
//全局前置守卫
router.beforeEach(async (to, from) => {
  // console.log('to====', to);
  // console.log('from====', from);
  let permissionStore = usePermission();
  //获取token携带凭证
  let token = sessionStorage.getItem('qf-token');
  //判断是否携带token,就是是否登入
  if (!token) {
    //判断当前是否在login页面,如果不是强制跳转到login页面
    if (to.path !== '/login') {
      return {
        path: '/login'
      };
    }
  } else {
    //调用store中的action方法发起异步请求

    //如果menulist的长度为0,那么就要发起请求
    if (permissionStore.menuList.length === 0) {
      await permissionStore.fetch_menuList();
      return {
        //废弃之前的路由,重新进入新添加的路由
        ...to
      };
    }
  }
});

//配置面包屑所用的全局后置钩子
router.afterEach((to, from) => {
  // console.log(to.matched);
  let breadCrumb = to.matched.filter((item) => item.path !== '/');
  // console.log(breadCrumb);
  let breadCrumbStore = useBreadCrumb();
  breadCrumbStore.$patch({
    datas: breadCrumb
  });
});
